#include <stdio.h>
#include "camera.h"
#include "serial.h"
#include "tcp_server_module.h"
#include <pthread.h>
#include <stdlib.h>
unsigned int w,h,size,indexs;
char *buf;
int camera_fd,connect_fd,ret,uart_fd;
pthread_t pth,pth_size,pth_jp;
int sig = 0;
char *bufs;
char jp_size[10];
unsigned char uart_buf[13] = {0};
pthread_mutex_t mutex;

void *camera_size(void *arg_size)
{
    while(1)
    {
        if(sig == 9)
            break;
        pthread_mutex_lock(&mutex);
        if(sig == 1)
        {
            printf("sig:%d\n",sig);
            sprintf(jp_size,"%d",size);
            tcp_server_send(connect_fd,(void *)jp_size,10);
            printf("传输大小\n");
            sig = 2;
            memset(jp_size,0,strlen(jp_size));
            usleep(20000);
        }
        pthread_mutex_unlock(&mutex); 
    }
}
void *camera_jp(void *arg_jp)
{
    while(1)
    {
        if(sig == 9)
            break;
        pthread_mutex_lock(&mutex);
        if(sig == 2)
        {
            printf("sig:%d\n",sig);
            tcp_server_send(connect_fd,(void *)bufs,size);
            printf("传输图片\n");
            sig = 0;
            memset(bufs,0,size);
            ret = camera_eqbuf(camera_fd,indexs);
            if(ret == -1)
            {       
                printf("qdbuf error\n");
                return 0;
            }
            usleep(20000);

        }
        pthread_mutex_unlock(&mutex);
    }   
}
void *camera(void *arg)
{	    
    printf("open camerasuccess\n");            
    camera_fd = camera_init("/dev/video0",&w,&h,&size);
    if(camera_fd == -1)
    {
        printf("open camera error\n");
        return 0;
    }

    int ret=camera_start(camera_fd );
    if(ret == -1)
    {
        printf("start error\n");
        return 0;
    }
    while(1)
    {
        if(sig == 9)
            break;
        pthread_mutex_lock(&mutex);
        if(sig == 0)
        {
            printf("sig:%d\n",sig);
            ret = camera_dqbuf(camera_fd,(void **)&bufs,&size,&indexs);
            if(ret == -1)
            {       
                printf("qdbuf error\n");
                return 0;
            }
            printf("出队\n");
            usleep(20000);
            sig = 1;
        }
        pthread_mutex_unlock(&mutex);
    }
    return 0;
}

int main(int argc, char *argv[])
{ 
    w = 640;
    h = 480;
    //*************tcp通讯初始化*********   
    int socket_fd = tcp_server_init(8888,10);
    if( -1==socket_fd)
    {
        perror("tcp_init failed");
        return -1;
    }
    connect_fd = tcp_server_wait_connect(socket_fd);
    if( -1==connect_fd)
    {
        perror("tcp_server_wait_connnect failed");
        return -1;
    }

    char buf[15] = {0};
    while(1)
    {
        tcp_server_recv(connect_fd,(void *)buf,sizeof(buf));
        printf("%s\n",buf);
        /* if(strcmp(buf,"open_uart")==0)
           {

           uart_fd = serial_init("/dev/ttyUSB0",115200);
           if(uart_fd == -1)
           {
           printf("uart open error\n");
           return -1;
           }
           }*/
        if(strcmp(buf,"open_camera")==0)
        {
            printf("进入线程\n");
            pthread_mutex_init(&mutex,NULL);
            pthread_create(&pth,NULL,camera,NULL);
            pthread_create(&pth_size,NULL,camera_size,NULL);
            pthread_create(&pth_jp,NULL,camera_jp,NULL);
        }
        if(strcmp(buf,"close_camera")==0)
        {
            printf("关闭摄像头\n");
            sig = 9;
            pthread_cancel(pth);
            pthread_cancel(pth_size);
            pthread_cancel(pth_jp);
            camera_stop(camera_fd);
            camera_exit(camera_fd);
        }
        if(strcmp(buf,"open_uart") == 0)
        {
            uart_fd = serial_init("/dev/ttyUSB0",115200);
            if(uart_fd == -1)
            {
                printf("open_camera\n");
                printf("open serial error\n");
                return -1;
            }
            while(1)
            {
                tcp_server_recv(connect_fd,(void *)uart_buf,3);
                if(strcmp(uart_buf,"un") == 0)
                    break;
                printf("测试阻塞\n");
                int i;
                for(i = 0; i < 3; i++)
                {
                    printf("uart_buf:%x\n",uart_buf[i]);
                }
                serial_send_exact_nbytes(uart_fd, uart_buf, 3);
                printf("serial send ok\n");
                bzero(uart_buf,sizeof(uart_buf));
            }


        }
        sleep(1);
        bzero(buf,sizeof(buf));

    }










    //*********串口***************************************
    /* char *devpath ="dev/video0";
       int serial_fd = serial_init(devpath,115200);
       if(serial_fd == -1)
       {
       perror("serial");
       return -1;
       }
       printf("serial init successful.............");
       */

    while(1);


    return 0;
} 
